/*
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ch_linghu.fanfoudroid.data;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.ch_linghu.fanfoudroid.R;
import com.ch_linghu.fanfoudroid.TwitterApplication;
import com.ch_linghu.fanfoudroid.fanfou.CommentStatus;
 
import com.ch_linghu.fanfoudroid.util.DateTimeHelper;
import com.ch_linghu.fanfoudroid.util.TextHelper;

public class Comment  implements Parcelable {
	private static final String TAG = "Comment";

	public com.ch_linghu.fanfoudroid.fanfou.User user;
	public String source;
	public String prevId;
	private int statusType = -1; // @see StatusTable#TYPE_*
	
	public String commentId;
	public String uid;
	public String content;
	public Date ctime;
	public String uname;
	public String face;
	 
	

	public void setStatusType(int type) {
		statusType = type;
	}

	public int getStatusType() {
		return statusType;
	}

	public Comment() {
	}

	public static Comment create(CommentStatus status) {
		Comment comment = new Comment();

		comment.commentId = status.getCommentId();
		comment.uid = status.getUid();
		 
		
		// 转义符放到getSimpleTweetText里去处理，这里不做处理
		comment.content = TextHelper.getSimpleTweetText(status.getContent());
		 
		comment.ctime = status.getCtime();
 
		comment.uname = status.getUname();
		comment.face = status.getFace();
		 
		 

	 
	//	tweet.source = TextHelper.getSimpleTweetText(status.getSource());

		return comment;
	}

	public static Comment createFromSearchApi(JSONObject jsonObject)
			throws JSONException {
		Comment comment = new Comment();

		comment.commentId = jsonObject.getString("id");
		comment.uid = jsonObject.getString("pid");
		// 转义符放到getSimpleTweetText里去处理，这里不做处理
		comment.content = jsonObject.getString("content");
 
		comment.ctime = DateTimeHelper.parseSearchApiDateTime(jsonObject
				.getString("ctime"));
		//tweet.favorited = jsonObject.getInt("favorited");
		comment.uname = jsonObject.getString("uname");
		comment.face = jsonObject.getString("face");
		 
	 
 

		return comment;
	}

	public static String buildMetaText(StringBuilder builder, Date createdAt,
			String source, String replyTo) {
		builder.setLength(0);

		builder.append(DateTimeHelper.getRelativeDate(createdAt));
		builder.append(" ");

		builder.append(TwitterApplication.mContext
				.getString(R.string.tweet_source_prefix));
		builder.append(source);

		if (!TextUtils.isEmpty(replyTo)) {
			builder.append(" "
					+ TwitterApplication.mContext
							.getString(R.string.tweet_reply_to_prefix));
			builder.append(replyTo);
			builder.append(TwitterApplication.mContext
					.getString(R.string.tweet_reply_to_suffix));
		}

		return builder.toString();
	}

	// For interface Parcelable

	public int describeContents() {
		return 0;
	}
 

	public void writeToParcel(Parcel out, int flags) {
		out.writeString(commentId);
		out.writeString(uid);
		out.writeString(content);
		out.writeValue(ctime); // Date
		out.writeString(uname);
		out.writeString(face);
	 

//		out.writeString(source);
	}

	public static final Parcelable.Creator<Comment> CREATOR = new Parcelable.Creator<Comment>() {
		public Comment createFromParcel(Parcel in) {
			return new Comment(in);
		}

		public Comment[] newArray(int size) {
			// return new Tweet[size];
			throw new UnsupportedOperationException();
		}
	};

	public Comment(Parcel in) {
		commentId = in.readString();
		uid = in.readString();
		content = in.readString();
		ctime = (Date) in.readValue(Date.class.getClassLoader());
		uname = in.readString();
		face = in.readString();
 
	}

	@Override
	public String toString() {
		return "Tweet [commentId=" + commentId + ", uid=" + uid + ", content="
				+ content + ", ctime=" + ctime + ", uname="
				+ uname + ", face=" + face
				+ "]";
	}

}
